home *** CD-ROM | disk | FTP | other *** search
- /**\
- |**| CPU_Meter.c
- \**/
-
- /**\
- |**| ==============================================================================
- |**| COMPILER DIRECTIVES
- |**| ==============================================================================
- \**/
-
- #define kMaxTasks 64
- #define kMaxCPUs 16 // Yea, I wish!
-
- #define SystemSevenOrLater 1
- #define DEBUGSTRINGS 1
-
- #define USE_COLLECTION_TASK 1
- #define USE_OFFSCREEN 0
- #define USE_ZOOM_WINDOW 0
-
- #define kInfoMPTaskWeight 9999
-
- /**\
- |**| ==============================================================================
- |**| INCLUDES
- |**| ==============================================================================
- \**/
-
- #ifndef USE_PRECOMPILED_HEADER
- #include "MyHeaders.i"
- #endif
-
- #include <Devices.h>
- #include <Fonts.h>
- #include <CodeFragments.h>
- #include <Dialogs.h>
- #include <DiskInit.h>
- #include <LowMem.h>
- #include <math.h>
- #include <Math64.h>
- #include <Multiprocessing.h>
- #include <PLStringFuncs.h>
- #include <Sound.h>
- #include <Traps.h>
- #include <Threads.h>
- #include <MacWindows.h>
-
- #include <stdio.h>
- #include <string.h>
-
- #include "MPInfoLib.h"
- #if USE_OFFSCREEN
- #include "Offscreen.h"
- #endif USE_OFFSCREEN
- #include "Preferences.h"
-
- #if __profile__
- #include <Profiler.h>
- #endif
-
- /**\
- |**| ==============================================================================
- |**| TYPEDEFS, STRUCTS, DEFINES, ENUMS, ETC.
- |**| ==============================================================================
- \**/
-
- #if DEBUGSTRINGS
- # define DEBUGSTR(x) DebugStr((x))
- # define LOGSTRING(b,s) if (b) do {DEBUGSTR(s);} while (false);
- #else
- # define DEBUGSTR(x)
- # define LOGSTRING(b,s)
- #endif
-
- enum
- {
- mAppleMenu = 128,
- iAboutBox = 1,
- mFileMenu = 129,
- iQuit = 1,
- mEditMenu = 130,
- iUndo = 1,
- iCut = 3,
- iCopy = 4,
- iPaste = 5,
- iClear = 6,
- iDuplicate = 8,
- iSelectAll = 9,
- mShow = 131,
- iProcID = 1,
- iTaskID = 2,
- iName = 3,
- iQueue = 4,
- iCPUTime = 5,
- iDelta = 6,
- iPercent = 7,
- iPreemptions = 8,
- iDeltaPreemptions = 9,
- iWeight = 10,
- iBar = 11,
- iSubTotals = 12,
- iTotals = 13,
- mInterval = 132,
- iPointFive = 1,
- iOne = 2,
- iTwo = 3,
- iFive = 4,
- iTen = 5,
- iTwenty = 6,
- iThirty = 7,
- iMinute = 8
- };
-
- #define kTextSize 12
-
- typedef struct MyTaskStruct
- {
- MPProcessID fProcID;
- MPTaskID fTaskID;
- MPTaskInfo fInfo;
- MPTaskInfo fLast;
- float fPercent;
- Boolean fValid;
- } MyTaskRec, * MyTaskPtr, ** MyTaskHdl;
-
- #define RECT_WIDTH(r) ((r).right - (r).left)
- #define RECT_HEIGHT(r) ((r).bottom - (r).top)
-
- #ifndef MIN
- # define ABS(x) ((x) >= 0 ? (x) : -(x))
- # define MIN(a,b) ((a) < (b) ? (a) : (b))
- # define MAX(a,b) ((a) > (b) ? (a) : (b))
- # define PIN(a,b,c) MIN(MAX((a),(b)),(c))
- #endif
-
- #if CALL_NOT_IN_CARBON
- #define EnableMenuItem(m,i) EnableItem((m),(i))
- #define InvalWindowRect(w,r) InvalRect(r)
- #define C2PSTR(s) C2PStr((Ptr) s)
- #else
- // #define C2PSTR(s) CopyCStringToPascal((const char *) s,(Str255) s)
- // #define C2PSTR(s) CopyCStringToPascal(s,(Str255) s)
- #define C2PSTR(s) CopyCStringToPascal((const char *) s,s)
- #endif
-
- /**\
- |**| ==============================================================================
- |**| EXPORTED FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
-
- static OSStatus Init_Mac(void);
- static OSStatus Open_Window(const Rect* pRect);
- static void Handle_Command(const long ms);
- static void Handle_Event(const EventRecord* pEventPtr);
- static void Handle_NullEvent(const EventRecord* pEventPtr);
- static void Handle_MouseEvent(const EventRecord* pEventPtr);
- static void Handle_KeyEvent(const char key,const SInt16 modifiers);
- static void Handle_UpdateEvent(WindowPtr updateWindowP);
- static void Handle_ActivateEvent(WindowPtr updateWindowP);
- static void Handle_DiskEvent(const long message);
- static void Handle_OSEvent(const long message);
-
- static Boolean SetUp_MenuBar(void);
- static void Adjust_MenuItems(void);
-
- static void Handle_ContentClick(WindowPtr pWindowPtr,const EventRecord* pEventPtr);
-
- static OSStatus Collect_Info(void);
- static void Draw_Info(void);
-
- static UInt64 AbsoluteToMilliseconds(const AbsoluteTime t);
- static OSStatus save_prefs(void);
-
- static RGBColor Blend_RGBColors(
- const RGBColor pRGBColorA,
- const RGBColor pRGBColorB,
- const float pBlend);
-
- #if 0
- static void Zoom_Window(WindowPtr theWindow,
- const short zoomDir,
- const short hMax,
- const short vMax);
- static void Grow_WindowGrid(WindowPtr theWindow);
- static void Drag_WindowGrid(WindowPtr win,const Point pt);
- #endif
-
- #if USE_COLLECTION_TASK
- static OSStatus InfoTask(void *parameter);
- #endif USE_COLLECTION_TASK
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE GLOBALS
- |**| ==============================================================================
- \**/
-
- static WindowPtr gWindowPtr = nil;
- static Rect gWindowRect;
- static Boolean gInBackGround = false;
- static Boolean gQuitFlag = false;
- #if USE_OFFSCREEN
- static Window_OffscreenPtr gOffscreenPtr = nil;
- #endif USE_OFFSCREEN
-
- static SInt32 gNextTicks = 0;
-
- static MyTaskRec gMyTaskRecs[kMaxTasks];
- static UInt32 gMyTaskRecCount = 0;
-
- static UInt32 gIntervals[] = {30, 60, 120, 300, 600, 1200, 1800, 3600};
-
- static const RGBColor
- blackRGBColor = {0x0000, 0x0000, 0x0000},
- blueRGBColor = {0x0000, 0x0000, 0xFFFF},
- ltBlueRGBColor = {0x7FFF, 0x7FFF, 0xFFFF},
- dkBlueRGBColor = {0x0000, 0x0000, 0x7FFF},
- redRGBColor = {0xFFFF, 0x0000, 0x0000},
- ltGreenRGBColor = {0x7FFF, 0xFFFF, 0x7FFF},
- greenRGBColor = {0x0000, 0xFFFF, 0x0000},
- dkGreenRGBColor = {0x0000, 0x7FFF, 0x0000},
- yellowRGBColor = {0xFFFF, 0xFFFF, 0x0000},
- dkGrayRGBColor = {0x4444, 0x4444, 0x5444},
- mdGrayRGBColor = {0x8888, 0x8888, 0x9888},
- ltGrayRGBColor = {0xaaaa, 0xaaaa, 0xbaaa},
- whiteRGBColor = {0xFFFF, 0xFFFF, 0xFFFF};
-
- static const RGBColor greenRGBColors[] = {
- {13107, 26214, 0},
- {39371, 54248, 15934},
- {46517, 57568, 16448},
- {52685, 60909, 28013},
- {56283, 65021, 30069},
- {60909, 65535, 47031}
- };
-
- static const RGBColor dkGreenRGBColors[] = {
- {6554, 131707, 0},
- {19686, 27124, 7967},
- {23259, 28784, 8224},
- {26343, 30455, 14007},
- {28142, 32511, 15035},
- {30455, 32768, 23516}
- };
-
- static const RGBColor grayRGBColors[] = {
- {21845, 21845, 21845},
- {34952, 34952, 34952},
- {43690, 43690, 43690},
- {48059, 48059, 48059},
- {56797, 56797, 56797},
- {61166, 61166, 61166}
- };
-
- static Preferences gPreferences;
-
- static EventRecord gTheEvent; // from the main event loop
- static Boolean gHasColorQD = true;
- static Point gGridSize = {8, 4};
-
- #if USE_COLLECTION_TASK
- static MPSemaphoreID gInfoReadyMPSemaphoreID = kInvalidID;
- static MPQueueID gMPNotifyQueueID = kInvalidID;
- static MPTaskID gInfoMPTaskID = kInvalidID;
- static OSErr gMPTaskErr = noErr;
- #endif USE_COLLECTION_TASK
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTIONS
- |**| ==============================================================================
- \**/
-
- void main(void)
- {
- #if __profile__
- if (!ProfilerInit(collectDetailed, bestTimeBase, 20, 5))
- {
- #endif
-
- if (!Init_Mac() && SetUp_MenuBar())
- {
- do
- {
- InitCursor();
- WaitNextEvent(everyEvent, &gTheEvent, 10, nil);
- Handle_Event(&gTheEvent);
- } while (!gQuitFlag);
- }
-
- #if __profile__
- }
- ProfilerDump("\pCPU_Meter.dump");
- ProfilerTerm();
- #endif
-
- }
-
- /**\
- |**| Initialize toolboxes
- \**/
-
- static OSStatus Init_Mac(void)
- {
- OSStatus anErr;
- #if !TARGET_API_MAC_CARBON
- SysEnvRec theWorld;
- //
- // Test the computer to be sure we can do color.
- // If not we would crash, which would be bad.
- // If we can’t run, just beep and exit.
- //
- anErr = SysEnvirons(1, &theWorld);
- if (theWorld.hasColorQD == false)
- {
- SysBeep(50);
- ExitToShell(); // If no color QD, we must leave.
- }
-
- MaxApplZone();
- InitGraf(&(qd.thePort));
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- #endif
- // InitContextualMenus();
- #if TARGET_API_MAC_CARBON
- {
- UInt32 tUInt32;
- GetDateTime(&tUInt32);
- SetQDGlobalsRandomSeed(tUInt32);
- }
- #else
- GetDateTime((unsigned long*) & qd.randSeed);
- #endif TARGET_API_MAC_CARBON
-
- gWindowPtr = nil; // just to be sure
-
- gPreferences.fVersion = -1; // illegal value
- anErr = ReadPreferences(&gPreferences);
- if (noErr == anErr)
- gWindowRect = gPreferences.fWindowRect;
-
- if (gPreferences.fVersion != kPrefsVersion)
- {
- #if TARGET_API_MAC_CARBON
- BitMap screenBits;
- gWindowRect = GetQDGlobalsScreenBits(&screenBits)->bounds;
- #else
- gWindowRect = qd.screenBits.bounds;
- #endif TARGET_API_MAC_CARBON
- gWindowRect.top += GetMBarHeight() + 1;
- InsetRect(&gWindowRect, 100, 100);
-
- gPreferences.fVersion = kPrefsVersion;
- gPreferences.fInterval = gIntervals[1];
- gPreferences.fShowProcID = false;
- gPreferences.fShowTaskID = false;
- gPreferences.fShowName = true;
- gPreferences.fShowQueue = true;
- gPreferences.fShowCPUTime = false;
- gPreferences.fShowDeltaCPU = true;
- gPreferences.fShowPercent = true;
- gPreferences.fShowPreemptions = false;
- gPreferences.fShowDeltaPreemptions = true;
- gPreferences.fShowWeight = true;
- gPreferences.fShowBar = true;
- gPreferences.fShowSubTotals = true;
- gPreferences.fShowTotals = true;
- save_prefs();
- }
-
- Open_Window(&gWindowRect);
-
- #if USE_COLLECTION_TASK
-
- if (!MPLibraryIsLoaded())
- return unimpErr;
-
- anErr = MPCreateSemaphore(256,0,&gInfoReadyMPSemaphoreID);
- if (noErr != anErr) return anErr;
-
- anErr = MPCreateQueue(&gMPNotifyQueueID);
- if (noErr != anErr) return anErr;
-
- anErr = MPCreateTask(InfoTask,nil,0,gMPNotifyQueueID,nil,&gMPTaskErr,0,&gInfoMPTaskID);
- if (noErr != anErr) return anErr;
-
- anErr = MPSetTaskWeight(gInfoMPTaskID,kInfoMPTaskWeight);
- if (noErr != anErr) return anErr;
-
- #endif USE_COLLECTION_TASK
-
- return noErr;
- } // Init_Mac
-
-
- /**\
- |**| Initialize toolboxes
- \**/
-
- static OSStatus Open_Window(const Rect* pRect)
- {
- //
- // Make a new window for drawing in, and it must be a color window.
- // The window is full screen size, made smaller to make it more visible.
- //
-
- gWindowPtr = NewCWindow(nil, pRect, "\pCPU Meter", true, zoomDocProc, (WindowPtr) - 1, false, 0);
-
- gWindowRect = *pRect;
- OffsetRect(&gWindowRect, -gWindowRect.left, -gWindowRect.top);
-
- SetPortWindowPort(gWindowPtr); // set window to current graf port
- TextSize(kTextSize); // smaller font for drawing.
-
- {
- short familyID;
- GetFNum("\pMonaco", &familyID);
- TextFont(familyID);
- }
- return noErr;
- }
-
- /**\
- |**| Setup menu bar
- \**/
-
- static Boolean SetUp_MenuBar(void)
- {
- Boolean result = false;
- Handle mBar = GetNewMBar(128); // handle to menu bar
-
- if (!ResError() && mBar)
- {
- SetMenuBar(mBar);
- #if !TARGET_API_MAC_CARBON
- AppendResMenu(GetMenuHandle(128), 'DRVR');
- #endif !TARGET_API_MAC_CARBON
- DrawMenuBar();
- ReleaseResource(mBar);
- result = true;
- }
- return result;
- }
-
- /**\
- |**| Adjust menu items
- \**/
-
- static void Adjust_MenuItems(void)
- {
- MenuHandle tMenuHdl = GetMenuHandle(mShow);
- if (tMenuHdl)
- {
- CheckMenuItem(tMenuHdl, iProcID, gPreferences.fShowProcID);
- CheckMenuItem(tMenuHdl, iTaskID, gPreferences.fShowTaskID);
- CheckMenuItem(tMenuHdl, iName, gPreferences.fShowName);
- CheckMenuItem(tMenuHdl, iQueue, gPreferences.fShowQueue);
- CheckMenuItem(tMenuHdl, iCPUTime, gPreferences.fShowCPUTime);
- CheckMenuItem(tMenuHdl, iDelta, gPreferences.fShowDeltaCPU);
- CheckMenuItem(tMenuHdl, iPercent, gPreferences.fShowPercent);
- CheckMenuItem(tMenuHdl, iPreemptions, gPreferences.fShowPreemptions);
- CheckMenuItem(tMenuHdl, iDeltaPreemptions, gPreferences.fShowDeltaPreemptions);
- CheckMenuItem(tMenuHdl, iWeight, gPreferences.fShowWeight);
- CheckMenuItem(tMenuHdl, iBar, gPreferences.fShowBar);
- CheckMenuItem(tMenuHdl, iSubTotals, gPreferences.fShowSubTotals);
- CheckMenuItem(tMenuHdl, iTotals, gPreferences.fShowTotals);
- }
-
- tMenuHdl = GetMenuHandle(mInterval);
- if (tMenuHdl)
- {
- SInt32 index, count = CountMenuItems(tMenuHdl);
- SInt32 interval = gPreferences.fInterval;
- SInt32 value;
- Boolean first = true;
-
- for (index = 1; index <= count; index++)
- {
- value = gIntervals[index - 1];
-
- EnableMenuItem(tMenuHdl, index);
-
- if (value < interval)
- CheckMenuItem(tMenuHdl, index, false);
- else
- {
- CheckMenuItem(tMenuHdl, index, first);
- if (first)
- gPreferences.fInterval = value;
- first = false;
- }
- }
- }
- }
-
- /**\
- |**| Do menu command
- \**/
-
- static void Handle_Command(const long ms)
- {
- short menuID = ms >> 16,
- menuItem = ms & 0xFFFF;
-
- switch (menuID)
- {
- case mAppleMenu:
- switch (menuItem)
- {
- case iAboutBox: // Bring up alert for About.
- SysBeep(15);
- break;
- default: // All non-About items in this menu are DAs.
- #if CALL_NOT_IN_CARBON
- {
- Str255 daName;
- GetMenuItemText(GetMenuHandle(menuID), menuItem, daName);
- OpenDeskAcc(daName);
- }
- #endif CALL_NOT_IN_CARBON
- break;
- }
- break;
- case mFileMenu:
- switch (menuItem)
- {
- case iQuit:
- gQuitFlag = true;
- break;
- }
- break;
- case mEditMenu:
- switch (menuItem)
- {
- case iUndo:
- break;
- case iCut:
- break;
- case iCopy:
- break;
- case iPaste:
- break;
- case iClear:
- break;
- case iDuplicate:
- break;
- case iSelectAll:
- break;
- }
- break;
- case mShow:
- switch (menuItem)
- {
- case iProcID:
- gPreferences.fShowProcID = !gPreferences.fShowProcID;
- break;
- case iTaskID:
- gPreferences.fShowTaskID = !gPreferences.fShowTaskID;
- break;
- case iName:
- gPreferences.fShowName = !gPreferences.fShowName;
- break;
- case iQueue:
- gPreferences.fShowQueue = !gPreferences.fShowQueue;
- break;
- case iCPUTime:
- gPreferences.fShowCPUTime = !gPreferences.fShowCPUTime;
- break;
- case iDelta:
- gPreferences.fShowDeltaCPU = !gPreferences.fShowDeltaCPU;
- break;
- case iPercent:
- gPreferences.fShowPercent = !gPreferences.fShowPercent;
- break;
- case iPreemptions:
- gPreferences.fShowPreemptions = !gPreferences.fShowPreemptions;
- break;
- case iDeltaPreemptions:
- gPreferences.fShowDeltaPreemptions = !gPreferences.fShowDeltaPreemptions;
- break;
- case iWeight:
- gPreferences.fShowWeight = !gPreferences.fShowWeight;
- break;
- case iBar:
- gPreferences.fShowBar = !gPreferences.fShowBar;
- break;
- case iSubTotals:
- gPreferences.fShowSubTotals = !gPreferences.fShowSubTotals;
- break;
- case iTotals:
- gPreferences.fShowTotals = !gPreferences.fShowTotals;
- break;
- }
- gNextTicks = TickCount(); // make it update now
- save_prefs();
- break;
- case mInterval:
- switch (menuItem)
- {
- case iPointFive:
- case iOne:
- case iTwo:
- case iFive:
- case iTen:
- case iTwenty:
- case iThirty:
- case iMinute:
- gPreferences.fInterval = gIntervals[menuItem - 1];
- gNextTicks = TickCount(); // make it update now
- save_prefs();
- default:
- break;
- }
- }
- }
-
- /**\
- |**| Handle event
- \**/
-
- static void Handle_Event(const EventRecord* pEventPtr)
- {
- switch (pEventPtr->what)
- {
- case nullEvent: // 0
- LOGSTRING(0, "\p|Handle_Event-I-Debug, nullEvent.;g");
- Handle_NullEvent(pEventPtr);
- break;
- case mouseDown: // 1
- LOGSTRING(0, "\p|Handle_Event-I-Debug, mouseDown.;g");
- Handle_MouseEvent(pEventPtr);
- break;
- case mouseUp: // 2
- LOGSTRING(0, "\p|Handle_Event-I-Debug, mouseUp.;g");
- break;
- case keyDown: // 3
- LOGSTRING(0, "\p|Handle_Event-I-Debug, keyDown.;g");
- goto dokey;
- break;
- case keyUp: // 4
- LOGSTRING(0, "\p|Handle_Event-I-Debug, keyUp.;g");
- break;
- case autoKey: // 5
- LOGSTRING(0, "\p|Handle_Event-I-Debug, autoKey.;g");
- dokey: Handle_KeyEvent((char)(pEventPtr->message & charCodeMask), pEventPtr->modifiers);
- break;
- case updateEvt: // 6
- LOGSTRING(0, "\p|Handle_Event-I-Debug, updateEvt.;g");
- Handle_UpdateEvent((WindowPtr)pEventPtr->message);
- break;
- case diskEvt: // 7
- LOGSTRING(0, "\p|Handle_Event-I-Debug, diskEvt.;g");
- Handle_DiskEvent(pEventPtr->message);
- break;
- case activateEvt: // 8
- LOGSTRING(0, "\p|Handle_Event-I-Debug, activateEvt.;g");
- Handle_ActivateEvent((WindowPtr)pEventPtr->message);
- break;
- case osEvt: // 15
- LOGSTRING(0, "\p|Handle_Event-I-Debug, osEvt.;g");
- Handle_OSEvent(pEventPtr->message);
- break;
- case kHighLevelEvent: // 23
- LOGSTRING(0, "\p|Handle_Event-I-Debug, kHighLevelEvent.;g");
- AEProcessAppleEvent(pEventPtr);
- break;
- default:
- LOGSTRING(0, "\p|pHandleEvent-I-Debug, UnHandled Event.;g");
- break;
- }
- }
-
- /**\
- |**| Do null event
- \**/
-
- static void Handle_NullEvent(const EventRecord* pEventPtr)
- {
- (pEventPtr);
-
- if (gWindowPtr)
- {
- GrafPtr savePort;
- #if !USE_COLLECTION_TASK
- SInt32 nowTicks = TickCount();
- #endif USE_COLLECTION_TASK
-
- GetPort(&savePort);
- SetPortWindowPort(gWindowPtr);
-
- #if USE_COLLECTION_TASK
- if (noErr == MPWaitOnQueue(gMPNotifyQueueID,nil,nil,nil,kDurationImmediate))
- DebugStr("\p|Handle_NullEvent-I-Debug, gMPNotifyQueueID notification!;");
-
- if (noErr == MPWaitOnSemaphore(gInfoReadyMPSemaphoreID,kDurationImmediate))
- #else
- if (nowTicks > gNextTicks)
- #endif USE_COLLECTION_TASK
- {
- #if !USE_COLLECTION_TASK
- gNextTicks = nowTicks + gPreferences.fInterval;
- #endif USE_COLLECTION_TASK
- InvalWindowRect(gWindowPtr,&gWindowRect);
- }
- SetPort(savePort);
- }
- }
-
- /**\
- |**| Do mousedown event
- \**/
-
- static void Handle_MouseEvent(const EventRecord* pEventPtr)
- {
- WindowPtr window;
- short part;
-
- part = FindWindow(pEventPtr->where, &window);
-
- if (part != inContent)
- {
- #if TARGET_API_MAC_CARBON
- Cursor tCursor;
- GetQDGlobalsArrow(&tCursor);
- SetCursor(&tCursor);
- #else
- SetCursor(&qd.arrow);
- #endif TARGET_API_MAC_CARBON
- }
-
- switch (part)
- {
- case inContent:
- Handle_ContentClick(window, pEventPtr);
- if (window != FrontWindow())
- {
- SelectWindow(window);
- // HiliteWindows();
- }
- else
- break;
-
- case inDrag:
- #if 0
- Drag_WindowGrid(window, pEventPtr->where);
- #else
- #if TARGET_API_MAC_CARBON
- {
- BitMap screenBits;
- DragWindow(window, pEventPtr->where, &GetQDGlobalsScreenBits(&screenBits)->bounds);
- }
- #else
- DragWindow(window, pEventPtr->where, &qd.screenBits.bounds);
- #endif
- #endif 0
- save_prefs();
- break;
-
- case inGoAway:
- if (TrackGoAway(window, pEventPtr->where))
- {
- save_prefs();
-
- DisposeWindow(window);
- // DisposeOneWindow(window,kClose);
- if (window == gWindowPtr)
- gWindowPtr = nil;
- }
- break;
-
- case inGrow:
- {
- #if 0
- Grow_WindowGrid(window);
- #else
- if (window == FrontWindow())
- {
- RgnHandle tGrayRgnHdl = GetGrayRgn();
- Rect growLimits;
- long growResult;
- #if ACCESSOR_CALLS_ARE_FUNCTIONS
- GetRegionBounds(tGrayRgnHdl,&growLimits);
- #else
- growLimits = (*tGrayRgnHdl)->rgnBBox;
- #endif
- growResult = GrowWindow(window, pEventPtr->where, &growLimits);
- if (growResult)
- {
- SizeWindow(window, LoWord(growResult), HiWord(growResult), true);
- #if USE_OFFSCREEN
- if (nil != gOffscreenPtr)
- Close_Offscreen(gOffscreenPtr);
- gOffscreenPtr = nil;
- #endif USE_OFFSCREEN
- }
- } else
- SelectWindow(window);
- #endif
- #if ACCESSOR_CALLS_ARE_FUNCTIONS
- GetPortBounds(GetWindowPort(window),&gWindowRect);
- #else
- gWindowRect = window->portRect;
- #endif CALL_NOT_IN_CARBON
- save_prefs();
- }
- break;
-
- case inMenuBar: // Process mouse menu command (if any).
- {
- long ms;
-
- Adjust_MenuItems();
- ms = MenuSelect(pEventPtr->where);
- if (ms)
- Handle_Command(ms);
- HiliteMenu(0); // Unhighlight what MenuSelect hilited.
- }
- break;
- #if CALL_NOT_IN_CARBON
- case inSysWindow: // Let the system handle the mouseDown.
- SystemClick(pEventPtr, window);
- break;
- #endif CALL_NOT_IN_CARBON
- case inZoomIn:
- case inZoomOut:
- {
- if (window == FrontWindow())
- {
- #if USE_ZOOM_WINDOW
- # if TARGET_API_MAC_CARBON
- BitMap screenBits;
- Rect zoomLimits = GetQDGlobalsScreenBits(&screenBits)->bounds;
- # else
- Rect zoomLimits = qd.screenBits.bounds;
- # endif TARGET_API_MAC_CARBON
-
- Zoom_Window(window, part, RECT_WIDTH(zoomLimits), RECT_HEIGHT(zoomLimits));
- #else
- if (!TrackBox(window, pEventPtr->where, part))
- break;
-
- ZoomWindow(window, part, false);
- # if ACCESSOR_CALLS_ARE_FUNCTIONS
- GetPortBounds(GetWindowPort(gWindowPtr),&gWindowRect);
- # else
- gWindowRect = gWindowPtr->portRect;
- # endif
- save_prefs();
- # if USE_OFFSCREEN
- if (nil != gOffscreenPtr)
- Close_Offscreen(gOffscreenPtr);
- gOffscreenPtr = nil;
- # endif USE_OFFSCREEN
- } else
- SelectWindow(window);
- #endif 0
- }
- break;
- default:
- break;
- }
- }
-
- /**\
- |**| Do key events
- \**/
-
- static void Handle_KeyEvent(const char key,const SInt16 modifiers)
- {
- if ((modifiers & cmdKey) != 0)
- {
- Adjust_MenuItems();
- // HandleMenuCommand(MenuKey(key));
- Handle_Command(MenuKey(key));
- HiliteMenu(0); // Unhighlight what MenuSelect hilited.
- }
- }
-
- /**\
- |**| Do update events
- \**/
-
- static void Handle_UpdateEvent(WindowPtr updateWindowP)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPortWindowPort(updateWindowP);
- BeginUpdate(updateWindowP);
-
- if (updateWindowP == gWindowPtr)
- {
-
- #if !USE_COLLECTION_TASK
- if (noErr != Collect_Info())
- DebugStr("\p|CPU_Meter-I-Debug, Handle_UpdateEvent:Collect_Info() error.;");
- #endif USE_COLLECTION_TASK
-
- // EraseRect(&gWindowRect);
- #if USE_OFFSCREEN
- if (nil == gOffscreenPtr)
- gOffscreenPtr = Open_Offscreen(gWindowPtr);
- SetPort_Offscreen(gOffscreenPtr);
- #endif USE_OFFSCREEN
- Draw_Info();
- #if USE_OFFSCREEN
- Copy_Onscreen(gOffscreenPtr);
- #endif USE_OFFSCREEN
- }
- #if TARGET_API_MAC_CARBON
- {
- RgnHandle tRgnHdl = nil;
-
- GetPortVisibleRegion(GetWindowPort(updateWindowP),tRgnHdl);
- UpdateControls(updateWindowP, tRgnHdl);
- }
- #else
- UpdateControls(updateWindowP, updateWindowP->visRgn);
- #endif TARGET_API_MAC_CARBON
-
- DrawGrowIcon(updateWindowP);
- EndUpdate(updateWindowP);
-
- SetPort(savePort);
- }
-
- /**\
- |**| Do disk events
- \**/
-
- static void Handle_DiskEvent(const long message)
- {
- #if CALL_NOT_IN_CARBON
- Point dialogLocation = {100, 100};
-
- if ((message & 0xFFFF0000) != noErr)
- {
- DIBadMount(dialogLocation, message);
- }
- #else
- (message);
- #endif CALL_NOT_IN_CARBON
- }
-
- /**\
- |**| Do Activate events
- \**/
-
- static void Handle_ActivateEvent(WindowPtr updateWindowP)
- {
- #pragma unused(updateWindowP)
- }
-
- /**\
- |**| Do OS events
- \**/
-
- static void Handle_OSEvent(const long message)
- {
- if ((message >> 24) == suspendResumeMessage)
- {
- if ((message & resumeFlag) != 0)
- gInBackGround = false;
- else
- gInBackGround = true;
- }
- else if ((message >> 24) == mouseMovedMessage)
- {
- // mouse moved!
- }
- }
-
- /**\
- |**| Do mouse down in window content
- \**/
-
- static void Handle_ContentClick(WindowPtr pWindowPtr,const EventRecord* pEventPtr)
- {
- #pragma unused (pEventPtr)
- if (pWindowPtr != FrontWindow())
- {
- SelectWindow(pWindowPtr);
- // HiliteWindows();
- }
- else
- {
- gNextTicks = TickCount(); // make it update now
- // SysBeep(15);
- }
- }
-
- static OSStatus Collect_Info(void)
- {
- MPTaskID tMPTaskID = kInvalidID;
- MPProcessID tMPProcessID = kInvalidID;
- MPTaskInfo tMPTaskInfo;
- OSStatus anErr = noErr;
- SInt32 count = 0;
-
- while (true)
- {
- tMPTaskInfo.version = kMPTaskInfoVersion;
-
- // For every kernel process...
- anErr = MPGetNextProcessID(&tMPProcessID);
- if (noErr != anErr)
- break;
-
- while (true)
- {
- SInt32 index;
-
- // For every task of this process...
- anErr = MPGetNextTaskID(tMPProcessID, &tMPTaskID);
- if (noErr != anErr)
- break;
-
- // get the task state (Duh!)
- anErr = MPExtractTaskState(tMPTaskID, kMPTaskStateTaskInfo, (void*) & tMPTaskInfo);
- if (noErr != anErr)
- break;
-
- count++; // Bump the count
-
- // find a task record that matches Proc & Task ID's.
- for (index = 0; index < gMyTaskRecCount; index++)
- {
- if ((tMPProcessID == gMyTaskRecs[index].fProcID) &&
- (tMPTaskID == gMyTaskRecs[index].fTaskID))
- break;
- }
-
- // If we didn't find a matching task record...
- if (index >= gMyTaskRecCount)
- { // search for an invalid (unused) task record
- for (index = 0; index < gMyTaskRecCount; index++)
- {
- if (kInvalidID == gMyTaskRecs[index].fTaskID)
- break;
- }
- }
-
- // If we still haven't found a task record to use...
- if (index >= gMyTaskRecCount)
- { // ... use the last record
- index = gMyTaskRecCount;
- if (gMyTaskRecCount < kMaxTasks)
- { // and bump the count
- gMyTaskRecs[index].fTaskID = kInvalidID;
- gMyTaskRecCount++;
- }
- }
-
- // if we found a task record to use...
- if (index < gMyTaskRecCount)
- {
- // ... store the new task record Info
- if (gMyTaskRecs[index].fTaskID == kInvalidID)
- {
- gMyTaskRecs[index].fProcID = tMPProcessID;
- gMyTaskRecs[index].fTaskID = tMPTaskID;
- gMyTaskRecs[index].fInfo = tMPTaskInfo;
- }
- gMyTaskRecs[index].fLast = gMyTaskRecs[index].fInfo;
- gMyTaskRecs[index].fInfo = tMPTaskInfo;
- gMyTaskRecs[index].fValid = true;
- }
- }
- }
- if ((0 != count) && (kMPInvalidIDErr == anErr))
- anErr = noErr;
-
- return anErr;
- }
-
- #if APPEARANCE_SAVY
- static void Draw_Info(void)
- {
-
- }
- #else
- static void Draw_Info(void)
- {
- Str255 tStr;
- Rect tRect = gWindowRect;
- UInt64 schedDelta, cpuTime, last_cpuTime, first_time, sched_time;
- UInt64 cpuTimeTotal[kMaxCPUs],cpuTimeGrandTotal = 0;
- UInt64 schedDeltaTotal[kMaxCPUs],schedDeltaGrandTotal = 0;
- UInt32 preemptions,preemptionsTotal[kMaxCPUs],preemptionsGrandTotal = 0;
- UInt32 deltaPreemptions,deltaPreemptionsTotal[kMaxCPUs],deltaPreemptionsGrandTotal = 0;
- UInt32 weight,weightTotal[kMaxCPUs],weightGrandTotal = 0;
- float percent,percentage,percentTotal[kMaxCPUs],percentGrandTotal = 0;
- Boolean validCPU[kMaxCPUs];
- SInt32 index;
-
- for (index = 0;index < kMaxCPUs;index++)
- {
- cpuTimeTotal[index] =
- schedDeltaTotal[index] =
- percentTotal[index] =
- preemptionsTotal[index] =
- deltaPreemptionsTotal[index] =
- weightTotal[index] = 0;
- validCPU[index] = 0;
- }
-
- // tRect.bottom = tRect.top + (kTextSize * 2);
- tRect.bottom = tRect.top + 20;
-
- sprintf((Ptr) tStr, " ");
- if (gPreferences.fShowProcID)
- sprintf((Ptr) tStr, "%s Proc ID ", tStr);
-
- if (gPreferences.fShowTaskID)
- sprintf((Ptr) tStr, "%s Task ID ", tStr);
-
- if (gPreferences.fShowName)
- sprintf((Ptr) tStr, "%sName ", tStr);
-
- if (gPreferences.fShowQueue)
- sprintf((Ptr) tStr, "%sQueue ", tStr);
-
- if (gPreferences.fShowCPUTime)
- sprintf((Ptr) tStr, "%s cpuTime ", tStr);
-
- if (gPreferences.fShowDeltaCPU)
- sprintf((Ptr) tStr, "%s ΔCPU ", tStr);
-
- if (gPreferences.fShowPercent)
- sprintf((Ptr) tStr, "%s cpu %% ", tStr);
-
- if (gPreferences.fShowPreemptions)
- sprintf((Ptr) tStr, "%s Preemptions ", tStr);
-
- if (gPreferences.fShowDeltaPreemptions)
- sprintf((Ptr) tStr, "%s ΔPreemp ", tStr);
-
- if (gPreferences.fShowWeight)
- sprintf((Ptr) tStr, "%s Weight", tStr);
-
- C2PSTR(tStr);
- RGBForeColor(&blackRGBColor);
- RGBBackColor(&grayRGBColors[5]);
-
- TETextBox(&tStr[1], tStr[0], &tRect, teFlushLeft);
-
- OffsetRect(&tRect, 0, 21);
-
- for (index = 0; index < gMyTaskRecCount; index++)
- {
- if (!gMyTaskRecs[index].fValid)
- gMyTaskRecs[index].fTaskID = kInvalidID;
- else if (gMyTaskRecs[index].fTaskID != kInvalidID)
- {
- SInt16 lastCPU = kMaxCPUs;
-
- RGBForeColor(&blackRGBColor);
- RGBBackColor(&grayRGBColors[5]);
-
- // creationTime time
- first_time = AbsoluteToMilliseconds(gMyTaskRecs[index].fInfo.creationTime);
-
- // current total CPU time
- cpuTime = AbsoluteToMilliseconds(gMyTaskRecs[index].fInfo.cpuTime);
-
- // last total CPU time
- last_cpuTime = AbsoluteToMilliseconds(gMyTaskRecs[index].fLast.cpuTime);
-
- // current scheduled CPU time
- sched_time = AbsoluteToMilliseconds(gMyTaskRecs[index].fInfo.schedTime);
-
- // Compute this interval's CPU percentage based on the last scheduled time.
- // If the task has not acquired any scheduling intervals, the CPU% is zero.
-
- schedDelta = AbsoluteToMilliseconds(gMyTaskRecs[index].fInfo.schedTime) - AbsoluteToMilliseconds(gMyTaskRecs[index].fLast.schedTime);
-
- if (schedDelta > 0)
- percent = ((cpuTime - last_cpuTime) * 100.0) / schedDelta;// instantaneous CPU %
- else
- percent = 0.0;
-
- if (sched_time > first_time)
- percentage = 100.0 * cpuTime / (sched_time - first_time);
- else
- percentage = 0.0;
-
- // We may have some error in our sampling, so pin it between 0 & 100 %.
- percent = fmax(fmin(percent, 100.0), 0.0);
- percentage = fmax(fmin(percentage, 100.0), 0.0);
-
- // sprintf((Ptr) tStr, " ");
-
- if (((gTheEvent.modifiers & optionKey) != optionKey) ||
- (gMyTaskRecs[index].fInfo.name != 'idle'))
- {
- tStr[0] = (gMyTaskRecs[index].fInfo.runState == kMPTaskRunning) ? '*' : ' ';
- tStr[1] = '\0';
-
- lastCPU = gMyTaskRecs[index].fInfo.lastCPU % kMaxCPUs;
- validCPU[lastCPU] = true;
- sprintf((Ptr) tStr, "%s%1.1X ", tStr, lastCPU);
-
- if (gPreferences.fShowProcID)
- sprintf((Ptr) tStr, "%s%08lx ", tStr, gMyTaskRecs[index].fProcID);
-
- if (gPreferences.fShowTaskID)
- sprintf((Ptr) tStr, "%s%08lx ", tStr, gMyTaskRecs[index].fTaskID);
-
- if (gPreferences.fShowName)
- sprintf((Ptr) tStr, "%s%.4s ", tStr, &gMyTaskRecs[index].fInfo.name);
-
- if (gPreferences.fShowQueue)
- sprintf((Ptr) tStr, "%s%.4s ", tStr, &gMyTaskRecs[index].fInfo.queueName);
-
- if (gPreferences.fShowCPUTime)
- sprintf((Ptr) tStr, "%s%3lld:%02lld:%02lld.%03lld ", tStr, cpuTime / (1000 * 60 * 60),// hours
- (cpuTime / (1000 * 60)) % 60,// minutes
- (cpuTime / 1000) % 60, // seconds
- cpuTime % 1000); // thousandths
-
- if (gPreferences.fShowDeltaCPU)
- sprintf((Ptr) tStr, "%s%6lldΔ ", tStr, schedDelta);
-
- if (gPreferences.fShowPercent)
- sprintf((Ptr) tStr, "%s %5.1f%% ", tStr, percent);
-
- preemptions = gMyTaskRecs[index].fInfo.preemptions;
- if (gPreferences.fShowPreemptions)
- sprintf((Ptr) tStr, "%s %8ld ", tStr, preemptions);
-
- deltaPreemptions = gMyTaskRecs[index].fInfo.preemptions - gMyTaskRecs[index].fLast.preemptions;
- if (gPreferences.fShowDeltaPreemptions)
- sprintf((Ptr) tStr, "%s %6ldΔ ", tStr, deltaPreemptions);
-
- weight = gMyTaskRecs[index].fInfo.weight;
- if (gPreferences.fShowWeight)
- sprintf((Ptr) tStr, "%s %6ld ", tStr, weight);
-
- C2PSTR(tStr);
- TETextBox(&tStr[1], tStr[0], &tRect, teFlushLeft);
-
- while (gPreferences.fShowBar)
- {
- float oldPercent = gMyTaskRecs[index].fPercent;
- Rect tRect1 = tRect, tRect2, tRect3;
- UInt32 width;
-
- tRect1.left += StringWidth(tStr) + 4;
- tRect1.right -= 20;
- InsetRect(&tRect1, 0, 2);
-
- if (tRect1.left >= tRect1.right)
- break;
-
- tRect1.bottom = tRect1.top + 11;
-
- RGBForeColor(&blackRGBColor);
- RGBBackColor(&grayRGBColors[3]);
-
- FrameRect(&tRect1);
- InsetRect(&tRect1, 1, 1);
-
- width = tRect1.right - tRect1.left;
-
- tRect3 = tRect2 = tRect1;
-
- tRect2.left = tRect1.right = tRect1.left + (width * percent * 0.01f);
- tRect3.left = tRect2.right = tRect1.left + (width * fmax(percent, oldPercent) * 0.01f);
- #if 0
- {
- UInt32 index = 0;
- Rect tRect4 = tRect1;
- while (tRect4.top < tRect4.bottom)
- {
- RGBForeColor(&greenRGBColors[index++]);
- FrameRect(&tRect4);
- InsetRect(&tRect4, 1, 1);
- }
- }
- #else
- {
- RGBColor tRGBColorMin = {0x4000,0xC000,0xFFFF};
- RGBColor tRGBColorMax = {0xFFFF,0xFFFF,0x1000};
- RGBColor tRGBColor = Blend_RGBColors(tRGBColorMin,tRGBColorMax,percent * 0.01f);
- if (percent < 100.0f)
- RGBForeColor(&tRGBColor);
- else
- RGBForeColor(&tRGBColorMax);
- PaintRect(&tRect1);
- }
-
- #endif 0
- if (percent < oldPercent)
- {
-
- UInt32 index = 0;
- while (tRect2.top < tRect2.bottom)
- {
- RGBForeColor(&dkGreenRGBColors[index++]);
- FrameRect(&tRect2);
- InsetRect(&tRect2, 1, 1);
- }
- }
-
- {
- UInt32 index;
- for (index = 0; index < 6; index++)
- {
- RGBForeColor(&grayRGBColors[index]);
- FrameRect(&tRect3);
- InsetRect(&tRect3, 1, 1);
- }
- PaintRect(&tRect3);
- }
-
- gMyTaskRecs[index].fPercent = percent;
-
- tRect1.right = (tRect1.left += (width * percentage / 100)) + 2;
-
- RGBForeColor(&redRGBColor);
- PaintRect(&tRect1);
-
- break;
- }
-
- OffsetRect(&tRect, 0, 21);
-
- cpuTimeTotal[lastCPU] += cpuTime;
- schedDeltaTotal[lastCPU] += schedDelta;
- percentTotal[lastCPU] += percent;
- preemptionsTotal[lastCPU] += preemptions;
- deltaPreemptionsTotal[lastCPU] += deltaPreemptions;
- weightTotal[lastCPU] += weight;
- }
- gMyTaskRecs[index].fValid = false;
- }
- }
-
- if (gPreferences.fShowSubTotals)
- {
- RGBForeColor(&blackRGBColor);
- RGBBackColor(&grayRGBColors[5]);
-
- sprintf((Ptr) tStr, " "); // 3 spaces
-
- if (gPreferences.fShowProcID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowTaskID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowName)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowQueue)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowCPUTime)
- sprintf((Ptr) tStr, "%s============= ", tStr);
-
- if (gPreferences.fShowDeltaCPU)
- sprintf((Ptr) tStr, "%s======= ", tStr);
-
- if (gPreferences.fShowPercent)
- sprintf((Ptr) tStr, "%s======= ", tStr);
-
- if (gPreferences.fShowPreemptions)
- sprintf((Ptr) tStr, "%s============= ", tStr);
-
- if (gPreferences.fShowDeltaPreemptions)
- sprintf((Ptr) tStr, "%s======== ", tStr);
-
- if (gPreferences.fShowWeight)
- sprintf((Ptr) tStr, "%s======== ", tStr);
-
- C2PSTR(tStr);
- TETextBox(&tStr[1], tStr[0], &tRect, teFlushLeft);
- OffsetRect(&tRect, 0, 21);
- }
-
- for (index = 0; index < kMaxCPUs; index++)
- {
- if (validCPU[index])
- {
- if (gPreferences.fShowSubTotals)
- {
- sprintf((Ptr) tStr, " %1.1X ", index);
-
- if (gPreferences.fShowProcID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowTaskID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowName)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowQueue)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowCPUTime)
- sprintf((Ptr) tStr, "%s%3lld:%02lld:%02lld.%03lld ",
- tStr,
- cpuTimeTotal[index] / (1000 * 60 * 60), // hours
- (cpuTimeTotal[index] / (1000 * 60)) % 60, // minutes
- (cpuTimeTotal[index] / 1000) % 60, // seconds
- cpuTimeTotal[index] % 1000); // thousandths
-
- if (gPreferences.fShowDeltaCPU)
- sprintf((Ptr) tStr, "%s%6lldΔ ", tStr, schedDeltaTotal[index]);
-
- if (gPreferences.fShowPercent)
- sprintf((Ptr) tStr, "%s %5.1f%% ", tStr, percentTotal[index]);
-
- if (gPreferences.fShowPreemptions)
- sprintf((Ptr) tStr, "%s %8ld ", tStr, preemptionsTotal[index]);
-
- if (gPreferences.fShowDeltaPreemptions)
- sprintf((Ptr) tStr, "%s %6ldΔ ", tStr, deltaPreemptionsTotal[index]);
-
- if (gPreferences.fShowWeight)
- sprintf((Ptr) tStr, "%s %6ld ", tStr, weightTotal[index]);
-
- C2PSTR(tStr);
- TETextBox(&tStr[1], tStr[0], &tRect, teFlushLeft);
- OffsetRect(&tRect, 0, 21);
- }
- cpuTimeGrandTotal += cpuTimeTotal[index];
- schedDeltaGrandTotal += schedDeltaTotal[index];
- percentGrandTotal += percentTotal[index];
- preemptionsGrandTotal += preemptionsTotal[index];
- deltaPreemptionsGrandTotal += deltaPreemptionsTotal[index];
- weightGrandTotal += weightTotal[index];
- }
- }
-
- if (gPreferences.fShowTotals)
- {
- RGBForeColor(&blackRGBColor);
- RGBBackColor(&grayRGBColors[5]);
-
- sprintf((Ptr) tStr, " "); // 3 spaces
-
- if (gPreferences.fShowProcID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowTaskID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowName)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowQueue)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowCPUTime)
- sprintf((Ptr) tStr, "%s============= ", tStr);
-
- if (gPreferences.fShowDeltaCPU)
- sprintf((Ptr) tStr, "%s======= ", tStr);
-
- if (gPreferences.fShowPercent)
- sprintf((Ptr) tStr, "%s======= ", tStr);
-
- if (gPreferences.fShowPreemptions)
- sprintf((Ptr) tStr, "%s============= ", tStr);
-
- if (gPreferences.fShowDeltaPreemptions)
- sprintf((Ptr) tStr, "%s======== ", tStr);
-
- if (gPreferences.fShowWeight)
- sprintf((Ptr) tStr, "%s======== ", tStr);
-
- C2PSTR(tStr);
- TETextBox(&tStr[1], tStr[0], &tRect, teFlushLeft);
-
- OffsetRect(&tRect, 0, 21);
-
- sprintf((Ptr) tStr, " "); // 3 spaces
-
- if (gPreferences.fShowProcID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowTaskID)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowName)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowQueue)
- sprintf((Ptr) tStr, "%s ", tStr);
-
- if (gPreferences.fShowCPUTime)
- sprintf((Ptr) tStr, "%s%3lld:%02lld:%02lld.%03lld ",
- tStr,
- cpuTimeGrandTotal / (1000 * 60 * 60), // hours
- (cpuTimeGrandTotal / (1000 * 60)) % 60, // minutes
- (cpuTimeGrandTotal / 1000) % 60, // seconds
- cpuTimeGrandTotal % 1000); // thousandths
-
- if (gPreferences.fShowDeltaCPU)
- sprintf((Ptr) tStr, "%s%6lldΔ ", tStr, schedDeltaGrandTotal);
-
- if (gPreferences.fShowPercent)
- sprintf((Ptr) tStr, "%s %5.1f%% ", tStr, percentGrandTotal);
-
- if (gPreferences.fShowPreemptions)
- sprintf((Ptr) tStr, "%s %8ld ", tStr, preemptionsGrandTotal);
-
- if (gPreferences.fShowDeltaPreemptions)
- sprintf((Ptr) tStr, "%s %6ldΔ ", tStr, deltaPreemptionsGrandTotal);
-
- if (gPreferences.fShowWeight)
- sprintf((Ptr) tStr, "%s %6ld ", tStr, weightGrandTotal);
-
- C2PSTR(tStr);
- TETextBox(&tStr[1], tStr[0], &tRect, teFlushLeft);
- OffsetRect(&tRect, 0, 21);
- }
-
- RGBBackColor(&grayRGBColors[5]);
- tRect.bottom = gWindowRect.bottom;
- EraseRect(&tRect);
-
- RGBForeColor(&blackRGBColor);
- tRect = gWindowRect;
- tRect.top -= 1;
- tRect.left -= 1;
- tRect.right -= 14;
- tRect.bottom -= 14;
- FrameRect(&tRect);
-
-
- }
- #endif APPEARANCE_SAVY
-
-
- //*************************
- //
- // AbsoluteToMilliseconds
- //
- //*************************
-
- static UInt64 AbsoluteToMilliseconds(const AbsoluteTime t)
- {
-
- Nanoseconds nSec;
- UInt64 oneMillion = 1000000;
-
- nSec = AbsoluteToNanoseconds(t);
-
- return U64Div(UnsignedWideToUInt64(nSec), oneMillion);
- }
-
- /**\
- |**| save_prefs
- \**/
-
- static OSStatus save_prefs(void)
- {
- gPreferences.fWindowRect = gWindowRect;
- if (gWindowPtr)
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPortWindowPort(gWindowPtr);
-
- LocalToGlobal((Point *) &gPreferences.fWindowRect.top);
- LocalToGlobal((Point *) &gPreferences.fWindowRect.bottom);
-
- SetPort(savePort);
- }
- return WritePreferences(&gPreferences);
- }
-
- /* ======================================================= */
- /* Routine: Blend_RGBColors */
- /* Purpose: blend two rgb colors according to the frac part of a float */
-
- static RGBColor Blend_RGBColors(
- const RGBColor pRGBColorA,
- const RGBColor pRGBColorB,
- const float pBlend)
- {
- RGBColor tRGBColor;
- SInt32 blend = pBlend;
- float fracB = pBlend - blend;
- float fracA = 1.0f - fracB;
-
- tRGBColor.red = (pRGBColorA.red * fracA) + (pRGBColorB.red * fracB);
- tRGBColor.green = (pRGBColorA.green * fracA) + (pRGBColorB.green * fracB);
- tRGBColor.blue = (pRGBColorA.blue * fracA) + (pRGBColorB.blue * fracB);
-
- return tRGBColor;
- }
-
- #if !TARGET_API_MAC_CARBON
- /**\
- |**| Zoom_Window
- \**/
-
- static void Zoom_Window(WindowPtr theWindow,
- const short zoomDir,
- const short hMax,
- const short vMax)
- {
- Rect *windRect = nil, *zoomRect = nil;
- Rect globalPortRect, theSect, dGDRect;
- GDHandle nthDevice, dominantGDevice;
- long sectArea,
- greatestArea;
-
- if (TrackBox(theWindow, gTheEvent.where, zoomDir))
- {
- SetPortWindowPort(theWindow);
- #if ACCESSOR_CALLS_ARE_FUNCTIONS
- GetPortBounds(GetWindowPort(theWindow),&globalPortRect);
- #else
- globalPortRect = theWindow->portRect;
- #endif
- EraseRect(&globalPortRect); // recommended for cosmetic reasons
-
- if (zoomDir == inZoomOut)
- {
- /*
- * ZoomWindow() is a good basic tool, but it doesn't do everything necessary to
- * implement a good human interface when zooming. In fact it's not even close for
- * more high-end hardware configurations. We must help it along by calculating an
- * appropriate window size and location any time a window zooms out.
- */
- #if TARGET_API_MAC_CARBON
- GetWindowBounds(theWindow, kWindowStructureRgn, windRect);
- #else
- windRect = &(**((WindowPeek)theWindow)->strucRgn).rgnBBox;
- #endif
- dominantGDevice = nil;
- if (gHasColorQD)
- {
-
- /*
- * Color QuickDraw implies the possibility of multiple monitors. This is where
- * zooming becomes more interesting. One should zoom onto the monitor containing
- * the greatest portion of the window. This requires walking the gDevice list.
- */
-
- nthDevice = GetDeviceList();
- greatestArea = 0;
- while (nthDevice != nil)
- {
- if (TestDeviceAttribute(nthDevice, screenDevice))
- {
- if (TestDeviceAttribute(nthDevice, screenActive))
- {
- SectRect(windRect, &(**nthDevice).gdRect, &theSect);
- sectArea = (long)RECT_WIDTH(theSect) * (long)RECT_HEIGHT(theSect);
- if (sectArea > greatestArea)
- {
- greatestArea = sectArea;// save the greatest intersection
- dominantGDevice = nthDevice;// and which device it belongs to
- }
- }
- }
- nthDevice = GetNextDevice(nthDevice);
- }
- }
-
- /*
- * At this point, we know the dimensions of the window we're zooming, and we know
- * what screen we're going to put it on. To be more specific, however, we need a
- * rectangle which defines the maximum dimensions of the resized window's contents.
- * This rectangle accounts for the thickness of the window frame, the menu bar, and
- * one or two pixels around the edges for cosmetic compatibility with ZoomWindow().
- */
-
- if (dominantGDevice != nil)
- {
- dGDRect = (**dominantGDevice).gdRect;
- if (dominantGDevice == GetMainDevice())// account for menu bar on main device
- dGDRect.top += GetMBarHeight();
- }
- else
- {
- #if TARGET_API_MAC_CARBON
- BitMap screenBits;
-
- dGDRect = GetQDGlobalsScreenBits(&screenBits)->bounds; // if no gDevice, use default monitor
- #else
- dGDRect = qd.screenBits.bounds; // if no gDevice, use default monitor
- #endif TARGET_API_MAC_CARBON
- dGDRect.top += GetMBarHeight();
- }
-
- #if TARGET_API_MAC_CARBON
- GetPortBounds(GetWindowPort(theWindow),&globalPortRect);
- #else
- globalPortRect = theWindow->portRect;
- #endif TARGET_API_MAC_CARBON
-
- LocalToGlobal(&topLeft(globalPortRect));// calculate the window's portRect
- LocalToGlobal(&botRight(globalPortRect));// in global coordinates
-
- // account for the window frame and inset it a few pixels
- dGDRect.left += 2 + globalPortRect.left - windRect->left;
- dGDRect.top += 2 + globalPortRect.top - windRect->top;
- dGDRect.right -= 1 + windRect->right - globalPortRect.right;
- dGDRect.bottom -= 1 + windRect->bottom - globalPortRect.bottom;
-
- /*
- * Now we know exactly what our limits are, and since there are input parameters
- * specifying the dimensions we'd like to see, we can move and resize the zoom
- * state rectangle for the best possible results. We have three goals in this:
- * 1. Display the window entirely visible on a single device.
- * 2. Resize the window to best represent the dimensions of the document itself.
- * 3. Move the window as short a distance as possible to achieve #1 and #2.
- */
- #if TARGET_API_MAC_CARBON
- GetWindowStandardState(theWindow, zoomRect);
- #else
- zoomRect = &(**(WStateDataHandle)((WindowPeek)theWindow)->dataHandle).stdState;
- #endif TARGET_API_MAC_CARBON
-
- /*
- * Initially set the zoom rectangle to the size requested by the input parameters,
- * although not smaller than a minimum size. We do this without moving the origin.
- */
-
- zoomRect->right = (zoomRect->left = globalPortRect.left) + hMax;
- // MAX(hMax, MinWindowWidth(theWindow));
- zoomRect->bottom = (zoomRect->top = globalPortRect.top) + vMax;
- // MAX(vMax, MinWindowHeight(theWindow));
-
- // Shift the entire rectangle if necessary to bring its origin inside dGDRect.
- OffsetRect(zoomRect, MAX(dGDRect.left - zoomRect->left, 0), MAX(dGDRect.top - zoomRect->top, 0));
-
- /*
- * Shift the rectangle up and/or to the left if necessary to accomodate the view,
- * and if it is possible to do so. The rectangle may not be moved such that its
- * origin would fall outside of dGDRect.
- */
-
- OffsetRect(zoomRect, -PIN(zoomRect->right - dGDRect.right, 0, zoomRect->left - dGDRect.left), -PIN(zoomRect->bottom - dGDRect.bottom, 0, zoomRect->top - dGDRect.top));
-
- // Clip expansion to dGDRect, in case view is larger than dGDRect.
- zoomRect->right = MIN(zoomRect->right, dGDRect.right);
- zoomRect->bottom = MIN(zoomRect->bottom, dGDRect.bottom);
- }
- ZoomWindow(theWindow, zoomDir, false); // all it needed was a brain transplant
- }
- }
-
- /**\
- |**| Grow_WindowGrid
- \**/
- #define kExtremeNeg -32768
- #define kExtremePos (32767 - 1) // required to address an old region bug, see develop 20 Q&As
-
- static void Pull_Rect(Rect* startRect); // prototype
- static void Grow_WindowGrid(WindowPtr theWindow)
- {
- PenState oldPen;
- GrafPtr tempWP, WMPort;
- Rect wideOpen = {kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos};
- RgnHandle oldRgn = NewRgn();
- Rect draggingRect;
-
- #if TARGET_API_MAC_CARBON
- GetPortBounds(GetWindowPort(theWindow),&draggingRect);
- #else
- draggingRect = theWindow->portRect;
- #endif TARGET_API_MAC_CARBON
-
- GetPort(&tempWP);
- SetPortWindowPort(theWindow);
-
- // normalize my rectangle into the window manager port coordinates
- LocalToGlobal((Point *) & draggingRect.top);
- LocalToGlobal((Point *) & draggingRect.bottom);
-
- // go to the WManager port
- #if TARGET_API_MAC_CARBON
- WMPort = *(GrafPtr*) 0x09DE;
- #else
- GetWMgrPort(&WMPort); //•• CALL NOT IN CARBON
- #endif TARGET_API_MAC_CARBON
- SetPort(WMPort);
-
- // save the Window manager pen state since we'll be changing it
- GetPenState(&oldPen);
-
- // localize back
- GlobalToLocal((Point *) & draggingRect.top);
- GlobalToLocal((Point *) & draggingRect.bottom);
-
- // save the current clip region, and set our wide-open clip
- GetClip(oldRgn);
- ClipRect(&wideOpen);
-
- // go to the routine below to do the actual dragging
- Pull_Rect(&draggingRect);
-
- // restore the original environment
- SetClip(oldRgn);
- SetPenState(&oldPen);
- DisposeRgn(oldRgn);
-
- // now size the window for the returned rect
- SetPortWindowPort(theWindow);
- #if TARGET_API_MAC_CARBON
- {
- Rect tRect;
- GetPortBounds(GetWindowPort(theWindow),&tRect);
- InvalWindowRect(theWindow,&tRect);
- }
- #else
- InvalWindowRect(theWindow,&theWindow->portRect);
- #endif TARGET_API_MAC_CARBON
- SizeWindow(theWindow, draggingRect.right - draggingRect.left, draggingRect.bottom - draggingRect.top, true);
- SetPort(tempWP);
- }
-
- static void Pull_Rect(Rect* startRect)
- {
- Rect oldRect;
- Point endPoint;
- Boolean hreversed = false;
- Boolean vreversed = false;
- short tempH,
- tempV;
- short divByGridh,
- divByGridv;
-
- // set up
- oldRect = *startRect;
- PenMode(srcXor); // So we can rubberband
- #if TARGET_API_MAC_CARBON
- {
- Pattern grayPat;
-
- GetQDGlobalsGray(&grayPat);
- PenPat(&grayPat);
- }
- #else
- PenPat(&qd.gray);
- #endif TARGET_API_MAC_CARBON
- FrameRect(startRect);
- divByGridh = startRect->right;
- divByGridv = startRect->bottom;
-
- while (StillDown())
- { // Keep doing this as long as the
- // user keeps the mouse down
-
- GetMouse(&endPoint); // Current mouse position in local
-
- // see if it's on a grid point
- tempH = ABS(endPoint.h - divByGridh) / gGridSize.h;
- tempV = ABS(endPoint.v - divByGridv) / gGridSize.v;
-
- // normalize to our grid values. We'll always go outwards as better
- if ((tempH * gGridSize.h) != ABS(endPoint.h - divByGridh))// shove out based on the remainer
- endPoint.h = (((endPoint.h) / gGridSize.h) * gGridSize.h) + gGridSize.h;
- if ((tempV * gGridSize.v) != ABS(endPoint.v - divByGridv))
- endPoint.v = (((endPoint.v) / gGridSize.v) * gGridSize.v) + gGridSize.v;
-
- // If things reversed, we have to make sure that we don't try
- // and grid the origin point of the drag, cuz that would be weird
- if (hreversed)
- {
- // see if the rectangle flipped first
- if (endPoint.h > startRect->right) // they flipped back
- hreversed = false; // and ignore this move
- else // still reversed
- startRect->left = endPoint.h;
- }
- else
- {
- if (endPoint.h < startRect->left)
- hreversed = true;
- else
- startRect->right = endPoint.h;
- }
- if (vreversed)
- {
- // see if it flipped first
- if (endPoint.v > startRect->bottom) // they flipped back
- vreversed = false; // and ignore this move
- else // still reversed
- startRect->top = endPoint.v;
- }
- else
- {
- if (endPoint.v < startRect->top)
- vreversed = true;
- else
- startRect->bottom = endPoint.v;
- }
-
- if (!EqualRect(startRect, &oldRect))
- {
- // redraw the rect only if the mouse moved
- FrameRect(&oldRect);
- FrameRect(startRect); // draw the new rect
- oldRect = *startRect;
- }
- }
- FrameRect(startRect);
-
- PenMode(srcCopy);
- #if TARGET_API_MAC_CARBON
- {
- Pattern blackPat;
-
- GetQDGlobalsBlack(&blackPat);
- PenPat(&blackPat);
- }
- #else
- PenPat(&qd.black);
- #endif TARGET_API_MAC_CARBON
- }
-
- /*-------------------------------------------------------------------------------------
-
- Drag_WindowGrid- a big nasty function to Drag a window along grid lines.
- Note the elegant error handling. You should change it to make your users happy.
-
- You can change the size of the 'grid rects' by changing the value of kIncrement.
-
- */
- static void Drag_WindowGrid(WindowPtr win,const Point pt)
- {
- RgnHandle dragRgn, lastDragRgn, insetGray;
- GrafPtr oldPort, tmpPort;
- Point currPt, firstMulPoint, lastMulPoint, currMulPoint;
- Boolean frameHidden;
- enum {kIncrement = 16, kBorderInset = 4};
-
- // Set up our regions - init our variables.
- dragRgn = NewRgn();
- lastDragRgn = NewRgn();
- insetGray = NewRgn();
- if ((dragRgn == NULL) || (lastDragRgn == NULL) || (insetGray == NULL))
- {
- DebugStr("\p|not enough memory- bye!");
- return;
- }
- CopyRgn(GetGrayRgn(), insetGray);
- if (MemError() != noErr)
- {
- DebugStr("\p|not enough memory- bye!");
- return;
- }
-
- InsetRgn(insetGray, kBorderInset, kBorderInset);
- frameHidden = false;
-
- // Set up a port to draw into, and save off the old
- #if TARGET_API_MAC_CARBON
- GetPort(&oldPort);
- tmpPort = CreateNewPort();
-
- SetPortVisibleRegion(tmpPort, GetGrayRgn());
- {
- Rect tRect;
-
- GetRegionBounds(GetGrayRgn(),&tRect);
- SetPortBounds(tmpPort,&tRect);
- }
- #else
- tmpPort = (GrafPtr) NewPtr(sizeof(GrafPort));
- if (tmpPort == NULL)
- {
- DebugStr("\p|not enough memory- bye!");
- return;
- }
- GetPort(&oldPort);
- OpenPort(tmpPort);
- CopyRgn(GetGrayRgn(), tmpPort->visRgn);
- if (MemError() != noErr)
- {
- DebugStr("\p|not enough memory- bye!");
- return;
- }
- tmpPort->portRect = (*GetGrayRgn())->rgnBBox;
- #endif TARGET_API_MAC_CARBON
- SetPort(tmpPort);
-
- PenMode(patXor);
- #if TARGET_API_MAC_CARBON
- {
- Pattern grayPat;
-
- GetQDGlobalsGray(&grayPat);
- PenPat(&grayPat);
- }
- #else
- PenPat(&qd.gray);
- #endif TARGET_API_MAC_CARBON
-
- // Set the incoming point to be on a multiple of kIncrement,
- // to make later calculations easier.
- currMulPoint.h = pt.h + (kIncrement / 2);
- currMulPoint.h /= kIncrement;
- currMulPoint.h *= kIncrement;
- currMulPoint.v = pt.v + (kIncrement / 2);
- currMulPoint.v /= kIncrement;
- currMulPoint.v *= kIncrement;
-
- firstMulPoint = lastMulPoint = currMulPoint;
- #if TARGET_API_MAC_CARBON
- if (noErr != GetWindowRegion(win,kWindowStructureRgn,dragRgn))
- #else
- CopyRgn(((WindowPeek)win)->strucRgn, dragRgn);
- if (MemError() != noErr)
- #endif TARGET_API_MAC_CARBON
- {
- DebugStr("\p|not enough memory- bye!");
- return;
- }
-
- #if TARGET_API_MAC_CARBON
- if (noErr != GetWindowRegion(win,kWindowStructureRgn,lastDragRgn))
- #else
- CopyRgn(((WindowPeek)win)->strucRgn, lastDragRgn);
- if (MemError() != noErr)
- #endif TARGET_API_MAC_CARBON
- {
- DebugStr("\p|not enough memory- bye!");
- return;
- }
-
- // Draw the first framed region, which will follow the mouse
- // on the screen
- FrameRgn(lastDragRgn);
-
- while (WaitMouseUp() == true)
- {
- // Now track the mouse, and when it moves enough make the framed
- // region move as well.
- GetMouse(&currPt);
-
- // Set the new point to be on a multiple of kIncrement
- currMulPoint.h = currPt.h + (kIncrement / 2);
- currMulPoint.h /= kIncrement;
- currMulPoint.h *= kIncrement;
- currMulPoint.v = currPt.v + (kIncrement / 2);
- currMulPoint.v /= kIncrement;
- currMulPoint.v *= kIncrement;
-
- // Should we be showing the frame region ??
- if (PtInRgn(currPt, insetGray) == false)
- {
- // It's somewhere near the edges, so hide the frame
- if (frameHidden == false) // if it's not hidden already, hide it now
- FrameRgn(lastDragRgn);
- frameHidden = true;
- }
- else
- { // else, the frame should be shown if it's hidden
- if (frameHidden == true)
- {
- FrameRgn(lastDragRgn);
- frameHidden = false;
- }
- }
-
- // Has the mouse moved ?
- if (!EqualPt(currMulPoint, lastMulPoint))
- {
- // The mouse coordinates have changed enough to adjust the window,
- // so move the frame accordingly
- OffsetRgn(dragRgn, currMulPoint.h - lastMulPoint.h, currMulPoint.v - lastMulPoint.v);
-
- if (frameHidden == false)
- {
- // Only show the frame if we're allowed to.
- FrameRgn(dragRgn);
- FrameRgn(lastDragRgn);
- }
- lastMulPoint = currMulPoint;
- CopyRgn(dragRgn, lastDragRgn);
- if (MemError() != noErr)
- {
- DebugStr("\p|not enough memory- bye!");
- return;
- }
- }
- }
-
- // If frameHidden is true, there's no need to erase the final frame.
- if (frameHidden == false)
- FrameRgn(lastDragRgn);
-
- if (!EqualPt(lastMulPoint, firstMulPoint) && (frameHidden == false))
- {
- // The mouse has moved from its original position and is
- // somewhere on the screen, so move the window accordingly.
- Point globalPt, diffPt, contPt = {
- 0, 0};
-
- // Calculate the difference between the strucRgn's 0, 0 and
- // the window's content region 0, 0. Remember that MoveWindow
- // moves the window's *content* to the coordinate specified, and
- // we want to move the window's structure to fit in the lastDragRgn
- SetPortWindowPort(win);
- // LocalToGlobal works much better when the port is set up...
- LocalToGlobal(&contPt);
- SetPort(tmpPort);
- #if TARGET_API_MAC_CARBON
- {
- Rect tRect;
-
- GetWindowBounds(win, kWindowStructureRgn, &tRect);
-
- diffPt.h = contPt.h - tRect.left;
- diffPt.v = contPt.v - tRect.top;
-
- GetRegionBounds(lastDragRgn,&tRect);
-
- globalPt.h = tRect.left;
- globalPt.v = tRect.top;
- }
- #else
- diffPt.h = contPt.h - (*((WindowPeek)win)->strucRgn)->rgnBBox.left;
- diffPt.v = contPt.v - (*((WindowPeek)win)->strucRgn)->rgnBBox.top;
- globalPt.h = (*lastDragRgn)->rgnBBox.left;
- globalPt.v = (*lastDragRgn)->rgnBBox.top;
- #endif TARGET_API_MAC_CARBON
-
- LocalToGlobal(&globalPt);
- globalPt.h += diffPt.h;
- globalPt.v += diffPt.v;
- MoveWindow(win, globalPt.h, globalPt.v, true);
- }
-
- // Close the port and tear everything down
- #if TARGET_API_MAC_CARBON
- DisposePort(tmpPort);
- #else
- ClosePort(tmpPort);
- #endif TARGET_API_MAC_CARBON
- SetPort(oldPort);
-
- DisposeRgn(dragRgn);
- DisposeRgn(insetGray);
- DisposeRgn(lastDragRgn);
- }
- #endif !TARGET_API_MAC_CARBON
-
- #if USE_COLLECTION_TASK
-
- static Duration TicksToMilliseconds(UInt32 pTicks)
- {
- return (pTicks * 500 * durationMillisecond / 30);
- }
-
- OSStatus InfoTask(void *parameter)
- {
- OSStatus anErr = noErr;
- AbsoluteTime expirationTime;
-
- (parameter); // #pragma unused
-
- expirationTime = UpTime();
-
- while (noErr == anErr)
- {
- anErr = Collect_Info();
- if (noErr != anErr) break;
-
- anErr = MPSignalSemaphore(gInfoReadyMPSemaphoreID);
- if (noErr != anErr) break;
-
- // calculate next expriation time
- expirationTime = AddDurationToAbsolute(TicksToMilliseconds(gPreferences.fInterval),expirationTime);
-
- // Now delay until our expiration time.
- anErr = MPDelayUntil(&expirationTime);
- }
- return anErr;
- }
- #endif USE_COLLECTION_TASK
-
-